package com.test.qnews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.test.qnews.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/19.
 */
public class WeatherAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> group;
    private List<List<String>> child;


    public WeatherAdapter(Context context) {
        this.context = context;
        initData();
    }
    private void initData() {

        group = new ArrayList<>();
        child = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            group.add("这是标题:" + i);
            List<String> subChild = new ArrayList<>();
            for (int j = 0; j < 20; j++) {
                subChild.add("这是标题" + i + "的第" + j + "项");
            }
            child.add(subChild);
        }

    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_exlistview_group,parent,false);
            groupViewHolder = new GroupViewHolder(convertView);
            convertView.setTag(groupViewHolder);
        }else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }
        groupViewHolder.group_tv.setText(group.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder childViewHolder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_exlistview_child,parent,false);
            childViewHolder = new ChildViewHolder(convertView);
            convertView.setTag(childViewHolder);
        }else {
            childViewHolder = (ChildViewHolder) convertView.getTag();
        }
        childViewHolder.child_tv.setText(child.get(groupPosition).get(childPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupViewHolder{
        TextView group_tv ;
        public GroupViewHolder(View view){
            group_tv = (TextView) view.findViewById(R.id.item_ex_group);

        }
    }
    class ChildViewHolder{
        TextView child_tv ;
        public ChildViewHolder(View view){
            child_tv = (TextView) view.findViewById(R.id.item_ex_child);

        }
    }

    public int getIndexFromString(String s){
        for (int i = 0; i <group.size() ; i++) {
            if (group.get(i).equals(s)){
                //找到了该字母,立即返回索引
                return i;
            }

        }
        //没有找到,返回-1
        return -1;
    }
}
